package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mdd.admin.service.ICompetitionSiteService;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.validate.competition.CompetitionSiteCreateValidate;
import com.mdd.admin.validate.competition.CompetitionSiteSearchValidate;
import com.mdd.admin.validate.competition.CompetitionSiteUpdateValidate;
import com.mdd.admin.vo.competition.CompetitionSiteListVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.competition.CompetitionSite;
import com.mdd.common.mapper.competition.CompetitionSiteMapper;
import com.mdd.common.util.TimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@Service
public class CompetitionSiteServiceImpl extends ServiceImpl<CompetitionSiteMapper, CompetitionSite> implements ICompetitionSiteService {

    @Resource
    CompetitionSiteMapper competitionSiteMapper;

    @Override
    public PageResult<CompetitionSiteListVo> list(PageValidate pageValidate, CompetitionSiteSearchValidate searchValidate) {
        Integer page = pageValidate.getPage_no();
        Integer limit = pageValidate.getPage_size();
        QueryWrapper<CompetitionSite> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(ObjectUtils.isNotEmpty(searchValidate.getId()),"competition_id",searchValidate.getId())
                        .eq(StringUtils.isNotEmpty(searchValidate.getSchedule()),"schedule",searchValidate.getSchedule());
        queryWrapper.orderByDesc("id");


        IPage<CompetitionSite> iPage = competitionSiteMapper.selectPage(new Page<>(page, limit), queryWrapper);
        List<CompetitionSiteListVo> list = new LinkedList<>();
        for (CompetitionSite item : iPage.getRecords()) {
            CompetitionSiteListVo vo = new CompetitionSiteListVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            list.add(vo);
        }
        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    @Override
    public void add(CompetitionSiteCreateValidate createValidate) {
        CompetitionSite model = new CompetitionSite();
        model.setName(createValidate.getName());
        model.setCompetitionId(createValidate.getCompetitionId());
        model.setAddress(createValidate.getAddress());
        model.setPlayType(createValidate.getPlayType());
        model.setSchedule(createValidate.getSchedule());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        competitionSiteMapper.insert(model);
    }

    @Override
    public void edit(CompetitionSiteUpdateValidate updateValidate) {
        CompetitionSite model = competitionSiteMapper.selectOne(
                new QueryWrapper<CompetitionSite>()
                        .eq("id", updateValidate.getId())
                        .last("limit 1"));

        Assert.notNull(model, "数据不存在!");
        model.setName(updateValidate.getName());
        model.setCompetitionId(updateValidate.getCompetitionId());
        model.setAddress(updateValidate.getAddress());
        model.setPlayType(updateValidate.getPlayType());
        model.setSchedule(updateValidate.getSchedule());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        competitionSiteMapper.updateById(model);
    }

    @Override
    public void del(Integer id) {
        CompetitionSite model = competitionSiteMapper.selectOne(
                new QueryWrapper<CompetitionSite>()
                        .eq("id", id)
                        .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        competitionSiteMapper.delete(new QueryWrapper<CompetitionSite>().eq("id", id));
    }

    @Override
    public CompetitionSite detail(Integer id) {
        return competitionSiteMapper.selectById(id);
    }
}
